﻿using System.Collections.Generic;
using System.Collections.Specialized;
using System.Web;

namespace Als.Utils.Pay.AliPay
{
    public static class Utils
    {
        //把请求参数打包成数组
        static SortedDictionary<string, string> sParaTemp = new SortedDictionary<string, string>();
        static Utils()
        {
            sParaTemp.Add("partner", Config.Partner);
            sParaTemp.Add("seller_email", Config.Seller_email);
            sParaTemp.Add("_input_charset", Config.Input_charset.ToLower());
            sParaTemp.Add("service", "create_direct_pay_by_user");
            sParaTemp.Add("payment_type", "1");            
        }
        public static string SendRequest(
            string out_trade_no, 
            string subject, 
            string body, 
            string total_fee, 
            string notify_url,
            string return_url,
            string show_url,
            string anti_phishing_key="",
            string exter_invoke_ip="") {
            sParaTemp.Add("notify_url", notify_url);
            sParaTemp.Add("return_url", return_url);
            sParaTemp.Add("out_trade_no", out_trade_no);
            sParaTemp.Add("subject", subject);
            sParaTemp.Add("total_fee", total_fee);
            sParaTemp.Add("body", body);
            sParaTemp.Add("show_url", show_url);
            sParaTemp.Add("anti_phishing_key", anti_phishing_key);
            sParaTemp.Add("exter_invoke_ip", exter_invoke_ip);
            return Submit.BuildRequest(sParaTemp, "get", "确认");
        }
        /// <summary>
        /// 异步调用验证签名的合法性
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public static bool VerifyNotify(HttpRequestBase request)
        {
            int i = 0;
            SortedDictionary<string, string> sArray = new SortedDictionary<string, string>();
            NameValueCollection coll;
            //Load Form variables into NameValueCollection variable.
            coll = request.Form;

            // Get names of all forms into a string array.
            System.String[] requestItem = coll.AllKeys;

            for (i = 0; i < requestItem.Length; i++)
            {
                sArray.Add(requestItem[i], request.Form[requestItem[i]]);
            }
            Notify aliNotify = new Notify();
            bool verifyResult = aliNotify.Verify(sArray, request.QueryString["notify_id"], request.QueryString["sign"]);
            return verifyResult;
        }
        /// <summary>
        /// 同步调用的时候用来验证签名是否合法
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public static bool VerifyReturn(HttpRequestBase request)
        {
            int i = 0;
            SortedDictionary<string, string> sArray = new SortedDictionary<string, string>();
            NameValueCollection coll;
            coll = request.QueryString;
            System.String[] requestItem = coll.AllKeys;
            for (i = 0; i < requestItem.Length; i++)
            {
                sArray.Add(requestItem[i], request.QueryString[requestItem[i]]);
            }
            Notify aliNotify = new Notify();
            bool verifyResult = aliNotify.Verify(sArray, request.QueryString["notify_id"], request.QueryString["sign"]);
            return verifyResult;
        }

    }
}
